package com.right.factory;

import io.netty.channel.Channel;
import io.netty.util.AttributeKey;

import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;


/**********************************************
 * @author pillar
 * @date 2018/5/24
 * T-BOX容器
 **********************************************/
public class NodeStatus {
    public static final AttributeKey<java.lang.String> LOCK_NO = AttributeKey.valueOf("lockNo");
    private static  NodeStatus global;
    private static ConcurrentHashMap<String, Channel> nodeStat;

    private NodeStatus(){
        nodeStat = new ConcurrentHashMap<>(100000,0.75f,16);
    }

    public static NodeStatus getInstance(){
        if(global == null){
            synchronized(NodeStatus.class){
                if(global == null){
                    global = new NodeStatus();
                }
            }
        }
        return global;
    }
    public Channel getClientStat(String key){
        return nodeStat.get(key);
    }

    public Channel getClientByImei(String imei){
        return nodeStat.get(imei);
    }
    /**
     * 新增客户机
     * @param key
     * @param value
     */
    public void putClientStat(String key, Channel value){
         value.attr(LOCK_NO).setIfAbsent(key);
        nodeStat.put(key, value);
    }

    /**
     * 移除客户机
     * @param key
     */
    public void remove(String key){
        nodeStat.remove(key);
    }

    public String getLockNo(Channel channel) {
        return channel.attr(LOCK_NO).get();
    }

    public Enumeration<String> getAllLockNo() {
        return nodeStat.keys();
    }
}
